/**
 * Object Script Start
 */

var canvasObj = function( canvasId ){
	
	this.viewCanvas = document.getElementById( canvasId );
	var buffCanvas = document.createElement('canvas');
	
	
	var ctx = this.viewCanvas.getContext('2d');
	var buffCtx = buffCanvas.getContext('2d');
	
	this.geometryObjList = new Array();
	
	this.clear = function(){
		debug( 'callcanvas clear' );
		buffCtx.clearRect(0, 0, this.viewCanvas.width + 1, this.viewCanvas.height);
		ctx.clearRect(0, 0, this.viewCanvas.width + 1, this.viewCanvas.height);
	}
	
	this.addGeoObj = function( pGeoObj ){
		this.geometryObjList.push( pGeoObj );
	}
	
	this.addGeoList = function( pGeoList ){
		this.geometryObjList = this.geometryObjList.concat( pGeoList );
	}
	
	this.drawAll = function(){
		this.clear();
		var geoObj = null;
		debug( 'geometryObjList[' + this.geometryObjList.length + ']' );
		while( this.geometryObjList.length > 0 ){
			geoObj = this.geometryObjList.pop();
			debug( 'geoObj[' + geoObj + ']' );
			if( geoObj.geoType == 'shape' ){
				this.drawShape( geoObj );
			}else{
				this.drawLine( geoObj );
			}
		}
		
		ctx.drawImage(buffCanvas, 0, 0);
		
	}
	
	this.drawLine = function( pLineObj ){
	    debug( 'callcanvas drawLine' );
	    var lvPointStart = null;
	    var lvPointEnd = null;
	    var lvLineObj = null;
	    
	    pLineObj.reset();
		buffCtx.beginPath();
		pLineObj.lineList.forEach(
			function( elm, idx ){
				if( idx == 0 ){
					lvPointStart = elm.posStart;
					buffCtx.moveTo(lvPointStart.posX, lvPointStart.posY);
				}
				lvPointEnd = elm.posEnd;
				buffCtx.lineTo(lvPointEnd.posX, lvPointEnd.posY);
			}
		);
		buffCtx.strokeStyle = pLineObj.lineColor;
		buffCtx.stroke();
	}


	this.drawShape = function( pShapeObj ){
	    debug( 'callcanvas drawShape' );
	    var lvPointStart = null;
	    var lvPointEnd = null;
	    var lvLineObj = null;
	    
	    pShapeObj.reset();
		buffCtx.beginPath();
		pShapeObj.lineList.forEach(
			function( elm, idx ){
				if( idx == 0 ){
					lvPointStart = elm.posStart;
					buffCtx.moveTo(lvPointStart.posX, lvPointStart.posY);
				}
				lvPointEnd = elm.posEnd;
				buffCtx.lineTo(lvPointEnd.posX, lvPointEnd.posY);
			}
		);
		buffCtx.strokeStyle = pShapeObj.lineColor;
		buffCtx.stroke();
	}
	
};
